package com.westcom.vulb.logic;

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.json.JSONException;
import org.json.JSONObject;

import com.westcom.vulb.network.http.VulbHttp;
import com.westcom.vulb.protocal.http.Response;

public class sheet extends BasicLogic 
{
	private static String CreateSheet_url = base_url+"web/create_sheet";
	private static String GetSheetList_url = base_url+"web/sheet_list";
	private static String GetSheetMemberList_url = base_url+"web/member_list";
	private static String GetSheetSetting_url = base_url+"web/member_list";
	private static String SetSheetSetting_url = base_url+"web/web/update_sheet_setting";
	private static String DeleteSheet_url = base_url+"web/delete_sheet";
	private static String UpdateSheetInfo_url = base_url+"web/update_sheet";
	private static String GetSheetShareUrl_url = base_url+"web/get_sheet_share_url";
	private static String AuthSheetShare_url = base_url+"web/group_regsit";
	private static String GetUnconfirmSheetList_url = base_url+"web/sheet_list_unconfirm";
	private static String GroupForceRetire_url = base_url+"web/group_force_retire";
	
	public static sheet getInstanse()
	{
		return new sheet();
	}
	
	public Response CreateSheet(String sessionId,String sheetName,boolean shareFlg,String confirmCd,String sheetDescription)
	{
		JSONObject j = new JSONObject();
        try 
        {
			j.put("sheetName", sheetName);
			j.put("shareFlg", shareFlg?"1":0);
	        j.put("confirmCd", (null != confirmCd&&confirmCd.length()>4)? confirmCd:null);
	        j.put("sheetDescription", sheetDescription);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        Response response = VulbHttp.post(CreateSheet_url,buildHeader(sessionId),builder.build());
        return response;
	}
	public Response GetSheetList(String sessionId)
	{
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", "");
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        Response response = VulbHttp.post(GetSheetList_url,buildHeader(sessionId),builder.build());
        return response;
	}
	public Response GetSheetMemberList(String sessionId,int sheetId)
	{
		JSONObject j = new JSONObject();
        try 
        {
			j.put("sheetId", sheetId);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        Response response = VulbHttp.post(GetSheetMemberList_url,buildHeader(sessionId),builder.build());
        return response;
	}
	public Response GetSheetSetting(String sessionId,int sheetId)
	{
		JSONObject j = new JSONObject();
        try 
        {
			j.put("sheetId", sheetId);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        Response response = VulbHttp.post(GetSheetSetting_url,buildHeader(sessionId),builder.build());
        return response;
	}
	public Response SetSheetSetting(String sessionId,int sheetId,boolean shareFlg,String confirmCd)
	{
		JSONObject j = new JSONObject();
        try 
        {
			j.put("sheetId", sheetId);
			j.put("shareFlg", shareFlg? "1":"0");
			j.put("confirmCd", confirmCd);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        Response response = VulbHttp.post(SetSheetSetting_url,buildHeader(sessionId),builder.build());
        return response;
	}
	public Response DeleteSheet(String sessionId,int[]sheetIds)
	{
		JSONObject j = new JSONObject();
        try 
        {
			j.put("sheetIds", intArray2JSONArray(sheetIds));
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        Response response = VulbHttp.post(DeleteSheet_url,buildHeader(sessionId),builder.build());
        return response;
	}
	public Response UpdateSheetInfo(String sessionId,int sheetId,String sheetName,String sheetDescription)
	{
		JSONObject j = new JSONObject();
        try 
        {
			j.put("sheetId", sheetId);
			j.put("sheetName", sheetName);
			j.put("sheetDescription", sheetDescription);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        Response response = VulbHttp.post(UpdateSheetInfo_url,buildHeader(sessionId),builder.build());
        return response;
	}
	public Response GetSheetShareUrl(String sessionId,int sheetId)
	{
		JSONObject j = new JSONObject();
        try 
        {
			j.put("sheetId", sheetId);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        Response response = VulbHttp.post(GetSheetShareUrl_url,buildHeader(sessionId),builder.build());
        return response;
	}
	
	public Response AuthSheetShare(String sessionId,int sheetId,String confirmCd)
	{
		JSONObject j = new JSONObject();
        try 
        {
			j.put("sheetId", sheetId);
			j.put("confirmCd", confirmCd);
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        Response response = VulbHttp.post(AuthSheetShare_url,buildHeader(sessionId),builder.build());
        return response;
	}
	
	public Response GetUnconfirmSheetList(String sessionId)
	{
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", "");
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        Response response = VulbHttp.post(GetUnconfirmSheetList_url,buildHeader(sessionId),builder.build());
        return response;
	}
	public Response GroupForceRetire(String sessionId,int sheetId,int[] userId)
	{
		JSONObject j = new JSONObject();
        try 
        {
			j.put("sheetId", sheetId);
        	j.put("userIds", intArray2JSONArray(userId));
			
		} catch (JSONException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("params", j.toString());
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        Response response = VulbHttp.post(GroupForceRetire_url,buildHeader(sessionId),builder.build());
        return response;
	}
}
